package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.orewar.OreWarNotice;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class OreWarNoticeDaoImpl extends TempDao {

    public LinkedList<OreWarNotice> getOreWarNoticeList(int activityId,int maxCount) {
        String sql = "select * from t_u_activity_orewar_notice where activityId = ? order by noticeTime desc limit ?";
        ResultSet rs = null;
        CallableStatement call = null;
        LinkedList<OreWarNotice> dataList = new LinkedList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            call.setInt(2, maxCount);
            rs = call.executeQuery();
            while (rs.next()) {
                dataList.addFirst(initOreWarNotice(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public boolean addOreWarNoticeList(List<OreWarNotice> noticeList) {
        String sql = "INSERT INTO t_u_activity_orewar_notice(noticeUid,activityId,pitId,noticeTime,userId,userBaseInfo,enemyUserId,enemyBaseInfo) VALUES(?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            boolean result = false;
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (OreWarNotice notice : noticeList) {
                Map<Integer, DbParameter> para = new HashMap<>();
                int i = 1;
                para.put(i++, new DbParameter(Types.NVARCHAR, notice.getNoticeUid()));
                para.put(i++, new DbParameter(Types.INTEGER, notice.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER, notice.getPitId()));
                para.put(i++, new DbParameter(Types.BIGINT, notice.getNoticeTime()));
                para.put(i++, new DbParameter(Types.BIGINT, notice.getUserId()));
                para.put(i++, new DbParameter(Types.NVARCHAR, notice.getUserBaseInfoJsonStr()));
                para.put(i++, new DbParameter(Types.BIGINT, notice.getEnemyUserId()));
                para.put(i++, new DbParameter(Types.NVARCHAR, notice.getEnemyBaseInfoJsonStr()));
                mapList.add(para);
            }

            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                result = true;
            }
            return result;
        }catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    private OreWarNotice initOreWarNotice(ResultSet rs) throws SQLException {
        OreWarNotice data = new OreWarNotice();
        data.setNoticeUid(rs.getString("noticeUid"));
        data.setActivityId(rs.getInt("activityId"));
        data.setPitId(rs.getInt("pitId"));
        data.setNoticeTime(rs.getLong("noticeTime"));
        data.setUserId(rs.getLong("userId"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setUserBaseInfo(userBaseInfo);
        }
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        baseUserInfoStr = rs.getString("enemyBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setEnemyBaseInfo(userBaseInfo);
        }
        return data;
    }
}
